package libofei.controller.admin.sysUser;

import com.github.pagehelper.PageHelper;
import libofei.common.controller.BaseController;
import libofei.entity.sysUser.SysUser;
import libofei.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminSysUserController extends BaseController {

    private static final String SYS_USER_LIST = getViewPath("admin/sysUser/sys_user_list");//系统用户主页
    private static final String SYS_USER_MODIFY = getViewPath("admin/sysUser/sys_user_add");//系统用户编辑

    private static final Logger logger = LoggerFactory.getLogger(AdminSysUserController.class);


    @Autowired
    private SysUserService sysUserService;


    @RequestMapping("/sys/user/list")
    public String sysUserIndex(HttpServletRequest request,
                               SysUser sysUser,
                               @RequestParam(value = "pageNum",defaultValue = "1")int pageNum) {
        try{
            PageHelper.startPage(pageNum, 10);
            List<SysUser> sysUserList = sysUserService.querySysUserList(sysUser);
            request.setAttribute("sysUserList",sysUserList);
            request.setAttribute("sysUser",sysUser);
        }catch (Exception e){
            logger.error("AdminSysUserController---sysUserIndex",e);
            throw new RuntimeException();
        }
        return SYS_USER_LIST;
    }

    @RequestMapping("/sys/user/to/modify")
    public String sysUserToModify(HttpServletRequest request, @RequestParam("id")Long id) {
        SysUser sysUser = new SysUser();
        if(id.longValue() > 0){
            sysUser = sysUserService.querySysUserById(id);
        }
        request.setAttribute("sysUser",sysUser);
        return SYS_USER_MODIFY;
    }

    @RequestMapping("/sys/user/modify")
    @ResponseBody
    public Map<String,Object> sysUserModify(SysUser sysUser) {
        try{
            Long cnt;
            if(ObjectUtils.isEmpty(sysUser.getId())){
                cnt = sysUserService.addUser(sysUser);
            }else{
                cnt = sysUserService.updateSysUser(sysUser);
            }
            if(cnt.longValue() > 0){
                return this.getJsonMap(true,"保存成功",null);
            }
        }catch (Exception e){
            logger.error("AdminSysUserController---sysUserModify",e);
        }
        return this.getJsonMap(false,"系统繁忙，请稍后再试",null);
    }


    @RequestMapping("/update/sys/user/pass")
    @ResponseBody
    public Map<String,Object> sysUserPass(@RequestParam("id") Long id,@RequestParam("pass")String pass) {
        try{
            Long cnt = sysUserService.updateSysUserPass(id,pass);
            if(cnt.longValue() > 0){
                return this.getJsonMap(true,"success",null);
            }
        }catch (Exception e){
            logger.error("AdminSysUserController---sysUserPass",e);
        }
        return this.getJsonMap(false,"系统繁忙，请稍后再试",null);
    }


    @RequestMapping("/update/sys/user/status")
    @ResponseBody
    public Map<String,Object> sysUserStatus(@RequestParam("id") Long id,@RequestParam("status")int status) {
        try{
            Long cnt = sysUserService.updateSysUserStatus(id,status);
            if(cnt.longValue() > 0){
                return this.getJsonMap(true,"success",null);
            }
        }catch (Exception e){
            logger.error("AdminSysUserController---sysUserStatus",e);
        }
        return this.getJsonMap(false,"系统繁忙，请稍后再试",null);
    }


    @RequestMapping("/check/isExist/user")
    @ResponseBody
    public Map<String,Object> checkIsExistUser(@RequestParam("username")String username) {
        Map<String,Object> json = this.getJsonMap(true,"success",null);
        try{
            SysUser sysUser = sysUserService.querySysUserByUsername(username);
            if(null != sysUser){
                json = this.getJsonMap(false,"fail",null);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("AdminSysUserController---checkIsExistUser",e);
        }
        return json;
    }
}
